//
//  MovieDetailViewController.m
//  UI-13-DouBan--
//
//  Created by lanou3g on 15/8/28.
//  Copyright (c) 2015年 自由科技. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "MovieDetailView.h"
#import "ImageDownloader.h"
#import <AVUser.h>
#import "LoginViewController.h"
#import "CollectionMovTableViewController.h"
#import "CollectionDataHandle.h"

@interface MovieDetailViewController ()

@end

@implementation MovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = self.movieModel.title;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back"] style:UIBarButtonItemStylePlain target:self action:@selector(backMovieList:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share@2x"] style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction:)];
    
    _movieDetailView = [[MovieDetailView alloc] initWithFrame:self.view.bounds];
    
    _movieDetailView.scrollView.frame = CGRectMake(0, 0, 375, 554);
//    //滚动视图的偏移量
    _movieDetailView.scrollView.contentSize = CGSizeMake(375, 280+[self textHeight:_movieModel.actors]+50 +[self textHeight:_movieModel.plot_simple]);
    //演员信息自适应
    _movieDetailView.actorsLabel.frame = CGRectMake(10, 270, 355, 10 +[self textHeight:_movieModel.actors]);
    
    _movieDetailView.mLabel.frame = CGRectMake(30, 285+[self textHeight:_movieModel.actors], 333, 30);
    //电影情节自适应
    _movieDetailView.plot_simpleLabel.frame = CGRectMake(10, 320+[self textHeight:_movieModel.actors], 355, [self textHeight:_movieModel.plot_simple]);
    //
    
    
    _movieDetailView.titleLabel.text = _movieModel.title;
    _movieDetailView.ratingLabel.text = [NSString stringWithFormat:@"评分:%@   (%@评论)",_movieModel.rating, _movieModel.rating_count];
    
    _movieDetailView.runtimeLabel.text = _movieModel.runtime;
    _movieDetailView.genresLabel.text = _movieModel.genres;
    _movieDetailView.countryLabel.text = _movieModel.country;
    _movieDetailView.actorsLabel.text = _movieModel.actors;
    _movieDetailView.plot_simpleLabel.text = _movieModel.plot_simple;
    
    
    //用get请求给img赋值
    NSMutableURLRequest *requ = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:_movieModel.poster]];
    NSData *data = [NSURLConnection sendSynchronousRequest:requ returningResponse:nil error:nil];
    UIImage *img = [UIImage imageWithData:data];
    self.movieDetailView.movieImageView.image = img;

#warning test 传图片
//    // LQH
//    self.movieModel.myImage = img;
//    self.movieDetailView.movieImageView.image = self.movieModel.myImage;
    
    [self.view addSubview:_movieDetailView];
}

//计算文本高度
- (CGFloat)textHeight:(NSString *)str{
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(355, 11111) options:NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    return newRect.size.height;
}
//返回首页
- (void)backMovieList:(UIBarButtonItem *)sender{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//收藏-LQH
- (void)rightAction:(UIBarButtonItem *)sender
{
    
    // 判断是否有账户登录
    if (![AVUser currentUser]) {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        
        [self presentViewController:loginVC animated:YES completion:^{
            
        }];
        
    }
    
    // 添加收藏
    
    // 打开数据库
    [[CollectionDataHandle sharedCollection] openDB];
    
    [[CollectionDataHandle sharedCollection] createTableMov];
    
    if ([[CollectionDataHandle sharedCollection] isExistMov:self.movieModel.title] == YES) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"已存在收藏夹" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
            
            sleep(0.5);
            [alert dismissViewControllerAnimated:true completion:^{
                
            }];
        }];

    } else {
        
        NSLog(@"%@",self.movieModel.movieId);
        
        [[CollectionDataHandle sharedCollection] insertMov:self.movieModel];
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"收藏成功" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
            
            sleep(0.5);
            [alert dismissViewControllerAnimated:true completion:^{
                
            }];
        }];

        
    }
    
    
    
    // 关闭数据库
    [[CollectionDataHandle sharedCollection] closeDB];

    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
